package com.rmb.userinfo.controller;

import com.alibaba.fastjson.JSON;
import com.rmb.common.model.response.CommonCode;
import com.rmb.common.model.response.ResponseResult;
import com.rmb.mart.entity.userinfo.UserInfo;
import com.rmb.userinfo.service.UserInfoService;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 段段
 * @PackageName: com.rmb.userinfo.controller
 * @ClassName: UserInfoController
 * @Description: (用户信息)表控制层
 * @data: 2021/1/20 15:18
 **/
@RestController
@RequestMapping("userInfo")
public class UserInfoController {
    /**
     * 服务对象
     */
    @Resource
    private UserInfoService userInfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public UserInfo selectOne(Long id) {
        return this.userInfoService.queryById(id);
    }

    /**
     * 查询用户基本信息
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public ResponseResult selectById(@PathVariable("id") Long id){
        Map<String,String> map = userInfoService.selectById(id);
        return map.size() > 0 ? new ResponseResult(CommonCode.SUCCESS, JSON.toJSONString(map)) :
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 修改密码
     * @param id 主键
     * @param password 新密码
     * @return
     */
    @PutMapping("/updatePassword/{id}")
    public ResponseResult updatePassword(@PathVariable("id") Long id,String password){
        return userInfoService.updatePassword(id,password) > 0 ?
                new ResponseResult(CommonCode.SUCCESS) :
                new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 修改密码
     * @param id 主键
     * @param phone 新手机号
     * @return
     */
    @PutMapping("/updatePhone/{id}")
    public ResponseResult updatePhone(@PathVariable("id") Long id,String phone){
        return userInfoService.updatePhone(id,phone) > 0 ?
                new ResponseResult(CommonCode.SUCCESS) :
                new ResponseResult(CommonCode.FAIL);
    }

}